/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view.components;

import org.apache.tapestry.*;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.InjectScript;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.apache.tapestry.services.ServiceMap;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Robert H. Engelhardt
 * Date: 4/20/11
 * Time: 8:36 AM
 */
public abstract class PieChart extends AbstractComponent {

    @InjectObject("service:tapestry.services.ServiceMap")
    public abstract ServiceMap getServiceMap();

    @InjectScript("script/pieChart.script")
    public abstract IScript getScript();

    @Parameter(required=true)
    public abstract String getService();

    @Parameter(required=true)
    public abstract Map<String, Object> getParameters();

    @Parameter(required=false, defaultValue="false")
    public abstract boolean isDisabled();

    private static final String DIVISION_ID = "divId";
    private static final String SERVICE_URL = "serviceUrl";

    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {

        if(!isDisabled() && !cycle.isRewinding()) {

            PageRenderSupport body = TapestryUtils.getOptionalPageRenderSupport(cycle);

            Map<String, Object> symbols = new HashMap<String, Object>();
            symbols.putAll(getParameters());
            symbols.put(DIVISION_ID, getClientId());
            symbols.put(SERVICE_URL, getServiceUrl());

            getScript().execute(this, cycle, body, symbols);

            writer.begin("div");
            writer.attribute("id", getClientId());
            renderInformalParameters(writer, cycle);
            writer.end();

        }

    }

    public String getServiceUrl() {
        IEngineService service = getServiceMap().getService(getService());
        ILink link = service.getLink(false, getParameters());
        return link.getURL();
    }

}
